#!/usr/bin/env node
import { readFile, writeFile } from 'fs/promises';
import { join } from 'path';
import { glob } from 'glob';          // 需先装：npm i -D glob

const esmDir = 'lib/esm';

(async () => {
  const files = await glob('**/*.mjs', { cwd: esmDir });
  for (const f of files) {
    const full = join(esmDir, f);
    let code = await readFile(full, 'utf8');
    // 把  from './xxx'  替换成  from './xxx.mjs'
    code = code.replace(/from\s+(['"])(\.\/[^'"]+)(?!\.\w+)\1/g, "from '$2.mjs'");
    await writeFile(full, code);
  }
  console.log('✅ ESM imports fixed');
})();